package models

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"news/conf"
)

var DB *gorm.DB
var err error

func Init() {
	Dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", conf.App.Mysql.User, conf.App.Mysql.Pass, conf.App.Mysql.Hose, conf.App.Mysql.Port, conf.App.Mysql.Database)
	DB, err = gorm.Open(mysql.Open(Dsn))
	if err != nil {
		fmt.Println("mysql连接失败")
		return
	}
	err = DB.AutoMigrate(&News{}, &Type{})
	if err != nil {
		fmt.Println("mysql数据表迁移失败")
		return
	}
	fmt.Println("预处理完成")
}
